#ifndef HERM_FRAMEWORK_HELPER_H
#define HERM_FRAMEWORK_HELPER_H

#include "herm/base/container/obj_mgr.h"
#include "herm/base/network/tcp_session.h"
#include "herm/base/network/nbc_tcp_session.h"

typedef herm::SingleObjMgr<TCPSession> TCPSessionObjMgr;
typedef herm::SingleObjMgr<NonblockConnectTCPSession> NBCTCPSessionObjMgr;

/**
 * @brief Get a unused session.
 */
template <class Session, class ObjMgr>
Session* AcquireSessionObj(ObjMgr* objMgr)
{
    Session* session = objMgr->AcquireObj();
    while (true)
    {
	    if (!session)
            break;

        if (!session->IsUsed()) 
            break;
        
        // The sessin is used, reacquire
        herm::ISession* tmpSession = reinterpret_cast<herm::ISession*>(session);
        objMgr->Release(tmpSession->GetIndex());
        session = objMgr->AcquireObj();
    }

    return session;
}

#endif
